#include "ConnectDialog.h"
#include "DbConnection.h"

ConnectDialog::ConnectDialog(QWidget *parent,
                             const QList<unit>&units):QDialog(parent),auth(0), m_units(units)
{
    QGridLayout *layout = new QGridLayout;
    usrLabel = new QLabel(tr("Имя пользователя:"));
    usrBox= new QComboBox;
    usrBox->setEditable(true);
    layout->addWidget(usrLabel,0,0);
    layout->addWidget(usrBox,0,1);

    pswLabel =new QLabel(tr("Пароль:"));
    psw = new QLineEdit;
    psw->setEchoMode(QLineEdit::Password);    
    layout->addWidget(pswLabel,1,0);
    layout->addWidget(psw,1,1);
    QCheckBox* showPass = new QCheckBox(tr("Показывать пароль"));
    connect(showPass,SIGNAL(stateChanged(int)),this,SLOT(stateChange(int)));
    layout->addWidget(showPass,2,1);

    connect(usrBox,SIGNAL(currentIndexChanged(int)),this,SLOT(onUserChange()));
    connect(usrBox,SIGNAL(editTextChanged(QString)),this,SLOT(onUserChange()));
    setUsrName(m_units.at(0).users);

    okButton = new QPushButton(tr("OK"));
    okButton->setEnabled((!usrBox->currentText().isEmpty())&&(!psw->text().isEmpty()));
    okButton->setDefault(true);
    cancelButton = new QPushButton(tr("Отмена"));
    connect(okButton,  SIGNAL(clicked()), this, SLOT(accept()));
    connect(cancelButton,  SIGNAL(clicked()),  this,  SLOT(cancelButtonCkick()));
    connect(cancelButton,  SIGNAL(clicked()),  this,  SLOT(reject()));    
    connect(psw,SIGNAL(textChanged(QString)),this,SLOT(okButttonEnabled()));

    QHBoxLayout  *rightLayout = new QHBoxLayout;
    rightLayout->addWidget(okButton);
    rightLayout->addWidget(cancelButton);

    layout->addLayout(rightLayout,6,1);
    this->setWindowTitle(tr("Авторизация"));
    this->setLayout(layout);
    this->setFixedHeight(sizeHint().height());
    this->setFixedWidth(sizeHint().width());
    if(usrBox->count()>0)
        psw->setFocus();
    else
        usrBox->setFocus();
}

bool  ConnectDialog::login()
{
    if(auth!=0) auth->~DbConnection();
    DbConnection::Parameters params;
    params.DbName="awsd";
    params.schema="oms";
    params.host="172.16.3.44";
    params.driver="QPSQL";
    params.port=5432;
    auth=new DbConnection(params);
    return auth->login(usrBox->currentText(),psw->text());
}

void ConnectDialog::setUsrName(const QStringList &users)
{
    usrBox->clear();
    usrBox->addItems(users);
}

int ConnectDialog::index() const
{
    return 0;
}
void ConnectDialog::okButttonEnabled()
{
    okButton->setEnabled(!psw->text().isEmpty());
}

void ConnectDialog::setCurUser(const QString &userName)
{
    int index=usrBox->findText(userName);
    if(index!=-1)
    {
        usrBox->setCurrentIndex(index);
    }
}

void ConnectDialog::setUsrName(const QString &userName)
{
    int index=usrBox->findText(userName);
    if(index==-1)
    {
        usrBox->addItem(userName);
    }
}

void ConnectDialog::onUnitChanged(const int &Index)
{
    setUsrName(m_units.at(Index).users);
}

DbConnection* ConnectDialog::db()
{
    return auth;
}
void ConnectDialog::onUserChange()
{
    psw->clear();
    //psw->setFocus();
}
void ConnectDialog::cancelButtonCkick()
{
    delete auth;
}

void ConnectDialog::stateChange(int state)
{
    if(state==Qt::Unchecked) psw->setEchoMode(QLineEdit::Password);
    if(state==Qt::Checked) psw->setEchoMode(QLineEdit::Normal);
}
